/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;

/**
 *
 * @author DK
 */
public class OntologyURIFindViewFrame extends JPanel implements ActionListener{
//    private MainFrame mf;
//    private Controller ctrl;
    private LearningInfoViewPanel livp;    
//    private JLabel sourceLabel;
    private JList uriList;
    private DefaultListModel uriListModel;
    private JButton setButton;
    private JFrame frame;
    private Vector<String> rset;
    private JLabel label;
    private String resultURI;
    private int type;
    
    private static final int CLASSTYPELIST = 0;
    private static final int PROPERTYTYPELIST = 1;
    
    
    public OntologyURIFindViewFrame(Vector<String> rset, LearningInfoViewPanel livp, int listType){

        this.livp = livp;
        this.frame = new JFrame();        
        this.frame = new JFrame("Select URI from List");
        this.frame.setSize(500, 300);    
//        this.label = new JLabel();
//        this.label.setText("URI List");
//        this.add(this.label);
        this.uriListModel = new DefaultListModel();
        this.uriList = new JList(uriListModel);
        this.uriList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.setButton = new JButton("Select URI");
        this.setButton.addActionListener(this);
        this.rset = rset;
        this.setBorder(BorderFactory.createTitledBorder("Select URI List"));
        this.addURISetToList(rset);
        this.type = listType;
        this.add(this.uriList);
        
        
        JScrollPane tscroll = new JScrollPane(this, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        this.add(this.setButton);
        this.frame.add(tscroll);
    }
    
    public void showFrame(){
        this.frame.setPreferredSize(getMaximumSize());
        this.frame.setVisible(true);
    }
    
    private String getSelectedResource(){
//        String s = new String();
        
        if(!this.uriList.isSelectionEmpty()){
            Object select = this.uriList.getSelectedValue().toString();
            String s = select.toString();
            return s;
        }
        
        
        return null;
    }
    
        
    private void addURISetToList(Vector<String> rset){
        for (int i = 0; i < rset.size(); i++) {
            this.uriListModel.addElement(rset.get(i));            
        }        
    }

    public void closeFrame(){
        this.frame.dispose();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        Object o = e.getSource();
        if(o==this.setButton){
            String result = this.getSelectedResource();
//            System.out.println("--- " + result);
//            this.resultURI = result
            if(result != null){
                if(this.type == OntologyURIFindViewFrame.CLASSTYPELIST){
                    this.livp.setTargetClassURI(result);
                }else if(this.type == OntologyURIFindViewFrame.PROPERTYTYPELIST){
                    this.livp.setTargetPropertyURI(result);
                }
                this.closeFrame();
            }else{
                
            
            }
            
//            System.out.println(this.resultURI);
            
        }
        
        
    }
    
}

